#
#
#
#

include ./Makefile.param

.PHONY: help clean all prepare show_param driver comp slave pc_tool tool	\
	setup_mini_uboot uboot busybox kernel link daemon app show_svn_change	\
	public check_svn_perm

# Add a little error checking
make_subdir = 									\
	@if [[ -e $(1) && -d $(1) ]]; then			\
	$(PUSHD) $(1); $(MAKE) $(2); $(POPD);		\
	else	\
	$(ECHO) "***Error: Folder $(1) not exist";	\
	exit -1;\
	fi

make_clean_subdir = 							\
	@if [[ -e $(1) && -d $(1) ]]; then			\
	$(PUSHD) $(1); $(MAKE) $(2) clean; $(POPD);	\
	else	\
	$(ECHO) "Warning: Folder $(1) not exist";	\
	fi

ALL_TARGET	= prepare link daemon kernel app driver public busybox uboot pc_tool

# Add slave chip
ifeq ($(SLAVE_CHIP), true)
ALL_TARGET	+= slave
endif

# Add mini uboot
ifeq ($(BUILD_MINI_UBOOT), true)
ALL_TARGET	+= setup_mini_uboot
endif

########################################
#
# Make upgrade.bin
#
########################################
all: $(ALL_TARGET)
	$(call make_subdir, $(OUTPUT_DIR))

########################################
#
# Make slave chip
#
########################################
slave: prepare show_svn_change
	$(CHMOD) u+x $(SETUP_SLV_SRC_SH)
	$(SETUP_SLV_SRC_SH)
	$(call make_subdir, $(SLAVE_CHIP_DIR))

########################################
#
# Setup and Make mini uboot
#
########################################
setup_mini_uboot: prepare show_svn_change
	$(CHMOD) u+x $(SETUP_MINI_UBOOT_SRC_SH)
	$(SETUP_MINI_UBOOT_SRC_SH)
	$(call make_subdir, $(MINI_UBOOT_DIR), MINI_UBOOT=y)

########################################
#
# Make upgrade_maker/restore_maker
# Must pass BOARD=x86 param
#
########################################
pc_tool:
	@if [[ -e $(PC_TOOL_DIR) && -d $(PC_TOOL_DIR) ]]; then	\
	$(PUSHD) $(PC_TOOL_DIR); $(MAKE) BOARD=x86; $(POPD);	\
	else	\
	$(ECHO) "***Error: Folder $(PC_TOOL_DIR) not exist";	\
	exit -1;\
	fi

########################################
#
# Link comp to binary excutable
# Make excutable sys
#
########################################
link: prepare comp tool show_svn_change
	$(call make_subdir, $(LINK_DIR))

########################################
#
# Make daemon 
#
########################################
daemon: prepare comp 
	$(call make_subdir, $(DAEMON_DIR))

########################################
#
# Make other program(udev/btols)
#
########################################
app: prepare 
	$(call make_subdir, $(APP_DIR))

########################################
#
# Make autotest subdirectory
#
########################################
#autotest: prepare comp
#	$(call make_subdir, $(AUTOTEST_DIR))

########################################
#
# Make uboot
#
########################################
uboot: prepare
	$(ECHO) "UBOOT_DIR="$(UBOOT_DIR)
	$(call make_subdir, $(UBOOT_DIR), PACK_MINI_UBOOT=$(PACK_MINI_UBOOT))

########################################
#
# Make busybox
#
########################################
busybox: prepare
	$(call make_subdir, $(BUSYBOX_DIR))

########################################
#
# Make kernel
#
########################################
kernel: prepare
	$(call make_subdir, $(KERNEL_DIR))

########################################
#
# Make our driver
#
########################################
driver: prepare kernel
	$(call make_subdir, $(DRIVER_DIR))

########################################
#
# Make our tool
#
########################################
tool: prepare
	$(call make_subdir, $(TOOL_DIR))

########################################
#
# Make our comp 
# minigui depends on tool/libpng and zlib
########################################
comp: prepare tool
	$(call make_subdir, $(COMP_DIR))

########################################
#
# First stage of rootfs/usrfs
#
########################################
public: prepare
	$(call make_subdir, $(PUBLIC_DIR))

########################################
#
# clean all
#
########################################
clean:
	$(call make_clean_subdir, $(LINK_DIR))
	$(call make_clean_subdir, $(DAEMON_DIR))
	$(call make_clean_subdir, $(APP_DIR))
	$(call make_clean_subdir, $(COMP_DIR))
	$(call make_clean_subdir, $(TOOL_DIR))
	$(call make_clean_subdir, $(DRIVER_DIR))
	$(call make_clean_subdir, $(KERNEL_DIR))
	$(call make_clean_subdir, $(UBOOT_DIR))
	$(call make_clean_subdir, $(BUSYBOX_DIR))
	$(call make_clean_subdir, $(OUTPUT_DIR))
	$(call make_clean_subdir, $(PUBLIC_DIR))
	@$(RM)	$(HBLIB_PATH)
	@$(RM)	$(MAKE_LOG_FILE)

########################################
#
# Make clean comp/build dir if 
# listed prequisit modified.
#
########################################
BUILD_STAMP_FILE = build_stamp.rej
$(BUILD_STAMP_FILE): Makefile.param $(AUTO_DEP_MK) $(PATH_DEF_MK) \
	$(COMMAND_DEF_MK) $(SDK_PATH_MK) $(BOARD_CONFIG_MK) $(TOOLCHAIN_CFG_MK)
	@$(ECHO) $? " changed, need to clean comp/build/tool/app dir"
	$(call make_clean_subdir, $(COMP_DIR))
	$(call make_clean_subdir, $(LINK_DIR))
	$(call make_clean_subdir, $(DAEMON_DIR))
	$(call make_clean_subdir, $(TOOL_DIR))
	$(call make_clean_subdir, $(APP_DIR))
	$(call make_clean_subdir, $(PUBLIC_DIR))
	$(call make_clean_subdir, $(PC_TOOL_DIR))
#	$(call make_clean_subdir, $(AUTOTEST_DIR))
ifeq ($(SLAVE_CHIP), true)
	$(call make_clean_subdir, $(SLAVE_CHIP_DIR))
endif
ifeq ($(SINGLE_NAND_FLASH), true)
	$(call make_clean_subdir, $(MINI_UBOOT_DIR))
endif
	@touch $@

include $(CLEAN_DIR_MK)
CLEAN_DIR_STAMP_FILE = clean_dir_stamp.rej
$(CLEAN_DIR_STAMP_FILE): $(CLEAN_DIR_MK)
	@$(ECHO) $? " changed, need to clean dir"
	@for x in $(DIR_TO_CLEAN);	\
	do			\
		make clean -C $$x;\
	done
	@touch $@

########################################
#
# check cli parameters
# 
########################################
prepare: $(BUILD_STAMP_FILE) $(CLEAN_DIR_STAMP_FILE) check_svn_perm
ifeq ($(BOARD),)
	@$(ECHO) "***Error: BOARD is not set"
	@$(ECHO) ""
	@$(MAKE) help
	@$(EXIT) -1
endif
	@$(MKDIR) -p $(HBLIB_PATH)
	@$(MKDIR) -p $(ATF_BIN_DIR)
	@$(MKDIR) -p $(ATF_CFG_DIR)

check_svn_perm: show_svn_change
	$(CHK_SVN_PERM_SH)

########################################
#
# Show last 50 svn commit log
# 
########################################
show_svn_change:
	$(SVN) log -l20 -v >svn_log_utf8.txt
	iconv -f UTF8 -t GBK svn_log_utf8.txt >svn_log.txt
	@$(PRINTF) "%-10s: %-30s\n" "BOARD" $(BOARD)		>  meta.txt
	@$(PRINTF) "%-10s: %-30s\n" "DEBUG" $(DEBUG)		>> meta.txt
	@$(PRINTF) "%-10s: %-30s\n" "PLATFORM" $(PLATFORM) 	>> meta.txt
	@$(PRINTF) "%-10s: %-30s\n" "CC" $(CC) 			 	>> meta.txt
	@$(PRINTF) "%-10s: %-30s\n" "SDK" $(HISIL_SDK)		>> meta.txt
	@$(PRINTF) "%-10s: %-30s\n" "URL" `$(SVN) info|grep "URL"|cut -c 6-`	>> meta.txt

ss:
	@$(CHMOD) u+x $(GET_SLV_SRC_DEV)
	@$(GET_SLV_SRC_DEV)


########################################
#
# parameter checker
# 
########################################
show_param:
	@$(ECHO) Hisilcon 3531 SDK: $(SDK_HISIL_3531)
	@$(ECHO) Hisilcon 3531 SDK: $(SDK_HISIL_3521)
	@$(ECHO) Target board:	${BOARD}
	@$(ECHO) Target kernel:	${KERNEL_DIR}
	@$(ECHO) Target SDK:	${SDK_DIR}

help:
	@$(ECHO) "====================Avaliable BOARD===================="
	@$(ECHO) "BOARD=HB9404  : For HB9404/HB9408"
	@$(ECHO) "BOARD=HB8608T3: For HB8608T3 Hisilicon 3521"
	@$(ECHO) "BOARD=HB8616T3: For HB8608T3 Hisilicon 3531"
	@$(ECHO) ""

include $(BUILD_MK)

# vim:noet:sw=4:ts=4

